<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>深浅拷贝</title>
</head>
<body>
<script>
    let obj = {
        name: "hah",
        age: 18,
        arr: [1,2,3], //0x000
        ob: {
            name: "test",
            age: 18,
            firends: ["test1", "test2"]
        }
    };
//    obj.arr//0x000
//    obj.ob//0x001
//    obj.ob.firneds//0x002


//浅拷贝

    let newObj = {};
    for(let key in obj){
        newObj[key] = obj[key];
        //newObj.name = obj.name
        //newObj.age = obj.age
        //newObj.arr = obj.arr
        //newObj.ob = obj.ob
    }
    console.log(newObj);

    /*newObj = {
        name: "hah",
        age: 18,
        arr: //0x000
        ob: //0x001
    };*/


    //浅拷贝 只拷贝第一层   深拷贝 层层拷贝
/*    let obj = {
        name: "hah",
        age: 18,
        arr: [1,2,3], //0x000
        ob: {
            name: "test",
            age: 18,
            firends: ["test1", "test2"]
        }
    };*/
//    obj.name = asdjjx
    /*
     * A {{obj.name+"经理"}}
     *
     * B {{}}
     *
     * */


    //手写深拷贝
    /**
     * 深拷贝
     */

    const obj1 = {
        age: 20,
        name: 'xxx',
        address: {
            city: 'beijing'
        },
        arr: ['a', 'b', 'c'],
        name2: null
    };

    function deepClone(obj = {}) {
        if (typeof obj !== 'object' || obj == null) {
            // obj 是 null ，或者不是对象和数组，直接返回
            return obj
        }

        // 初始化返回结果
        let result;
        if (obj instanceof Array) {
            result = []
        } else {
            result = {}
        }

        for (let key in obj) {
            // 保证 key 不是原型的属性
            if (obj.hasOwnProperty(key)) {
                // 递归调用！！！
                result[key] = deepClone(obj[key])
            }
        }

        // 返回结果
        return result
    }

    function deepClone(obj) {
        if(typeof obj !== 'object' || obj == null){//20
            // obj 是 null ，或者不是对象和数组，直接返回
            return obj //20
        }

        // 初始化返回结果 [1,2,3]  {a: 1}
        let result;
        if (obj instanceof Array) {
            result = []
        } else {
            result = {}
        }

        for(let key in obj){
            //newObj.arr = obj.arr,
            //newObj.obj = obj.obj,
            result[key] = deepClone(obj[key]);//20
        }
        return result
    }



    const obj1 = {
        age: 20,
        name: 'xxx',
        address: {
            city: 'beijing',
            address: {
                city: 'beijing'
            },
        },
        arr: ['a', 'b', 'c']
    };

    let newObj = {};
    for(let key in obj){//  key = address  /arr
        if(typeof obj1[key] !== "object"){
            newObj[key] = obj1[key];
        }else {//[] {}
            newObj[key] = clone(obj1[key])
        }
    }

    function clone(obj) {//address : {city: 'beijing'}  / ['a', 'b', 'c']
        if(typeof obj !== "object"){
            return obj;
        }
        let newObj = {};
        if(obj instanceof Array){
            newObj = []
        }
        for(let key in obj){//  key = address
            newObj[key] = clone(obj[key]);
        }
        return newObj
    }

</script>
</body>
</html>